package com.weiyi.mashangzhu.Landlord.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.Landlord.Adapter.NextAdapter2;
import com.weiyi.mashangzhu.Landlord.Bean.NextBean;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HouseTypeActivity extends BaseActivity {

    @BindView(R.id.lv)
    ListView lv;
    List<NextBean> list = new ArrayList<>();
    NextAdapter2 nextAdapter2 = null;
    String info,id;
    String type="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house_type);
        ButterKnife.bind(this);
        Intent intent=getIntent();
        info=intent.getStringExtra("info");
        id=intent.getStringExtra("id");
        list.add(new NextBean("民居", R.mipmap.choices1));
        list.add(new NextBean("公寓", R.mipmap.choices1));
        list.add(new NextBean("独栋别墅", R.mipmap.choices1));
        list.add(new NextBean("四合院", R.mipmap.choices1));
        list.add(new NextBean("客栈", R.mipmap.choices1));
        list.add(new NextBean("阁楼", R.mipmap.choices1));
        list.add(new NextBean("其他", R.mipmap.choices1));
        if (info.equals("post")){
            if (MyApp.type!=null){
                if (MyApp.type.equals("1")){
                    list.get(0).setImg(R.mipmap.choices);
                }else  if (MyApp.type.equals("2")){
                    list.get(1).setImg(R.mipmap.choices);
                }else  if (MyApp.type.equals("3")){
                    list.get(2).setImg(R.mipmap.choices);
                }else  if (MyApp.type.equals("4")){
                    list.get(3).setImg(R.mipmap.choices);
                }else  if (MyApp.type.equals("5")){
                    list.get(4).setImg(R.mipmap.choices);
                }else  if (MyApp.type.equals("6")){
                    list.get(5).setImg(R.mipmap.choices);
                }else  if (MyApp.type.equals("7")){
                    list.get(6).setImg(R.mipmap.choices);
                }
            }
            nextAdapter2 = new NextAdapter2(list, this);
            lv.setAdapter(nextAdapter2);
        }else {
            LoadingDialog.show(this);
            OkHttpUtils.get()
                    .addParams("token", SPUtils.get(this,"token","")+"")
                    .addParams("id",id)
                    .url(API.BaseApi_L+API.GetHouseType)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {
                            LoadingDialog.dismiss(HouseTypeActivity.this);
                            T.showShort(HouseTypeActivity.this,"数据获取失败，请稍后再试");

                        }

                        @Override
                        public void onResponse(String response) {
                            LoadingDialog.dismiss(HouseTypeActivity.this);
                            try {
                                JSONObject jsonObject=new JSONObject(response);
                                if (jsonObject.getInt("status")==0){
                                    JSONArray jsonArray=jsonObject.getJSONArray("data");
                                    JSONObject jsonObject1=jsonArray.getJSONObject(0);

                                    type=jsonObject1.getString("hType");
                                    if (!type.equals("")){
                                        Log.e("hhhhhhhh","type="+type);
                                        if (type.equals("1")){
                                            list.get(0).setImg(R.mipmap.choices);
                                        }else  if (type.equals("2")){
                                            list.get(1).setImg(R.mipmap.choices);
                                        }else  if (type.equals("3")){
                                            list.get(2).setImg(R.mipmap.choices);
                                        }else  if (type.equals("4")){
                                            list.get(3).setImg(R.mipmap.choices);
                                        }else  if (type.equals("5")){
                                            list.get(4).setImg(R.mipmap.choices);
                                        }else  if (type.equals("6")){
                                            list.get(5).setImg(R.mipmap.choices);
                                        }else  if (type.equals("7")){
                                            list.get(6).setImg(R.mipmap.choices);
                                        }
                                    }
//                                    list.get(Integer.parseInt(type)-1).setImg(R.mipmap.choices);
                                    nextAdapter2 = new NextAdapter2(list, HouseTypeActivity.this);
                                    lv.setAdapter(nextAdapter2);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });
        }


        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (info.equals("post")){
                    for (int i = 0; i <position ; i++) {
                        list.get(i).setImg(R.mipmap.choices1);
                    }
                    for (int i = position+1; i <list.size() ; i++) {
                        list.get(i).setImg(R.mipmap.choices1);
                    }
                    list.get(position).setImg(R.mipmap.choices);
                    type=position+1+"";
//                    MyApp.type=position+1+"";
                    nextAdapter2.notifyDataSetChanged();
                }else {
                    for (int i = 0; i <position ; i++) {
                        list.get(i).setImg(R.mipmap.choices1);
                    }
                    for (int i = position+1; i <list.size() ; i++) {
                        list.get(i).setImg(R.mipmap.choices1);
                    }
                    list.get(position).setImg(R.mipmap.choices);
                    type=position+1+"";
//                    T.showShort(HouseTypeActivity.this,"=="+type);
                    nextAdapter2.notifyDataSetChanged();
                }

            }
        });

    }


    @OnClick({R.id.back, R.id.baocun})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.baocun:
                if (info.equals("post")){
                    Intent intent = new Intent();
                    if (type==null){
                        T.showShort(this,"请选择房源类型");
                    }else {
                        MyApp.type=type;
//                        Log.e("xxxxxxxxxxxxxxxx","tupe="+MyApp.type);
                        intent.putExtra("info", "完成");
                        setResult(0x009, intent);
                        finish();
                    }
                }else {
                    Log.e("xxxxxxxxxxxxxxxx","tupe="+type);
                    LoadingDialog.show(this);
                    OkHttpUtils.get()
                            .addParams("token",SPUtils.get(HouseTypeActivity.this,"token","")+"")
                            .addParams("id",id)
                            .addParams("hType",type)
                            .url(API.BaseApi_L+API.UpdateHouseType)
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Request request, Exception e) {
                                    LoadingDialog.dismiss(HouseTypeActivity.this);
                                    T.showShort(HouseTypeActivity.this,"数据获取失败，请稍后再试");

                                }
                                @Override
                                public void onResponse(String response) {
                                    LoadingDialog.dismiss(HouseTypeActivity.this);
                                    try {
                                        JSONObject jsonObject=new JSONObject(response);
                                        if (jsonObject.getInt("status")==0){
                                            T.showShort(HouseTypeActivity.this,jsonObject.getString("msg") + " 后台审核中...");
                                            finish();
                                        } else {
                                            T.showShort(HouseTypeActivity.this,jsonObject.getString("msg"));
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }

                                }
                            });

                }

                break;
        }
    }
}
